package chess

import (
	"reflect"
)

const (
	MaxLine = 10
	MinLine = 1

	MaxRow = 9
	MinRow = 1
)

//var AChessboard *Chessboard
//
//func init() {
//	AChessboard = new(Chessboard)
//}

func CreateChessboard() *Chessboard {
	c := new(Chessboard)
	c.M = make(map[Point]MoveChess)
	c.initIt()
	return c
}

func (c *Chessboard) initIt() {
	c.initChariot()
	c.initHorse()
	c.initElephant()
	c.initGuard()
	c.initKing()
	c.initCannon()
	c.initSoldier()
	c.initMap()
}

//func init&Piece(point Point) (piece interface{}) {
//	if (point.Row == 0 && point.Line == 0) || (point.Row == 8 && lin) {
//	}
//}

func (c *Chessboard) initCannon() {
	c.RedCannon1 = &Cannon{
		&Piece{
			Color:  "red",
			Point:  Point{8, 2},
			Status: true,
			Name:   "RedCannon",
		},
	}
	//c.RedCannon1.piece = c.RedCannon1
	c.RedCannon2 = &Cannon{
		&Piece{
			Color:  "red",
			Point:  Point{8, 8},
			Status: true,
			Name:   "RedCannon",
		},
	}
	//c.RedCannon2.piece = c.RedCannon2
	c.BlaCannon1 = &Cannon{
		&Piece{
			Color:  "black",
			Point:  Point{3, 2},
			Status: true,
			Name:   "BlaCannon",
		},
	}
	//c.BlaCannon1.piece = c.BlaCannon1
	c.BlaCannon2 = &Cannon{
		&Piece{
			Color:  "black",
			Point:  Point{3, 8},
			Status: true,
			Name:   "BlaCannon",
		},
	}
	//c.BlaCannon2.piece = c.BlaCannon2
}

func (c *Chessboard) initChariot() {
	c.BlaChariot1 = &Chariot{
		&Piece{
			Color:  "black",
			Point:  Point{1, 1},
			Status: true,
			Name:   "BlaChariot",
		},
	}

	c.BlaChariot2 = &Chariot{
		&Piece{Color: "black",
			Point:  Point{1, 9},
			Status: true,
			Name:   "BlaChariot",
		},
	}
	c.RedChariot1 = &Chariot{
		&Piece{Color: "red",
			Point:  Point{10, 1},
			Status: true,
			Name:   "RedChariot",
		},
	}
	c.RedChariot2 = &Chariot{
		&Piece{Color: "red",
			Point:  Point{10, 9},
			Status: true,
			Name:   "RedChariot",
		},
	}
}

func (c *Chessboard) initHorse() {
	c.BlaHorse1 = &Horse{
		&Piece{Color: "black",
			Point:  Point{1, 2},
			Status: true,
			Name:   "BlaHorse",
		},
	}
	c.BlaHorse2 = &Horse{
		&Piece{Color: "black",
			Point:  Point{1, 8},
			Status: true, Name: "BlaHorse"},
	}
	c.RedHorse1 = &Horse{
		&Piece{Color: "red",
			Point:  Point{10, 2},
			Status: true,
			Name:   "RedHorse",
		},
	}
	c.RedHorse2 = &Horse{
		&Piece{Color: "red",
			Point:  Point{10, 8},
			Status: true,
			Name:   "RedHorse",
		},
	}
}

func (c *Chessboard) initElephant() {
	c.BlaElephant1 = &Elephant{
		&Piece{Color: "black",
			Point:  Point{1, 3},
			Status: true, Name: "BlaElephant"},
	}
	c.BlaElephant2 = &Elephant{
		&Piece{Color: "black",
			Point:  Point{1, 7},
			Status: true, Name: "BlaElephant"},
	}
	c.RedElephant1 = &Elephant{
		&Piece{Color: "red",
			Point:  Point{10, 3},
			Status: true, Name: "RedElephant"},
	}
	c.RedElephant2 = &Elephant{
		&Piece{Color: "red",
			Point:  Point{10, 7},
			Status: true, Name: "RedElephant"},
	}
}

func (c *Chessboard) initGuard() {
	c.BlaGuards1 = &Guard{
		&Piece{Color: "black",
			Point:  Point{1, 4},
			Status: true,
			Name:   "BlaGuards",
		},
	}
	c.BlaGuards2 = &Guard{
		&Piece{Color: "black",
			Point:  Point{1, 6},
			Status: true,
			Name:   "BlaGuards",
		},
	}
	c.RedGuards1 = &Guard{
		&Piece{Color: "red",
			Point:  Point{10, 4},
			Status: true,
			Name:   "RedGuards",
		},
	}
	c.RedGuards2 = &Guard{
		&Piece{Color: "red",
			Point:  Point{10, 6},
			Status: true, Name: "RedGuards"},
	}
}

func (c *Chessboard) initKing() {
	c.BlaKing = &King{
		&Piece{Color: "black",
			Point:  Point{1, 5},
			Status: true,
			Name:   "BlaKing",
		},
	}
	c.RedKing = &King{
		&Piece{Color: "red",
			Point:  Point{10, 5},
			Status: true,
			Name:   "RedKing",
		},
	}
}

func (c *Chessboard) initSoldier() {
	c.BlaSoldier1 = &Soldier{
		&Piece{Color: "black",
			Point:  Point{4, 1},
			Status: true,
			Name:   "BlaSoldier",
		},
	}
	c.BlaSoldier2 = &Soldier{
		&Piece{Color: "black",
			Point:  Point{4, 3},
			Status: true,
			Name:   "BlaSoldier",
		},
	}
	c.BlaSoldier3 = &Soldier{
		&Piece{Color: "black",
			Point:  Point{4, 5},
			Status: true, Name: "BlaSoldier"},
	}
	c.BlaSoldier4 = &Soldier{
		&Piece{Color: "black",
			Point:  Point{4, 7},
			Status: true, Name: "BlaSoldier"},
	}
	c.BlaSoldier5 = &Soldier{
		&Piece{Color: "black",
			Point:  Point{4, 9},
			Status: true, Name: "BlaSoldier"},
	}
	c.RedSoldier1 = &Soldier{
		&Piece{Color: "red",
			Point:  Point{7, 1},
			Status: true, Name: "RedSoldier"},
	}
	c.RedSoldier2 = &Soldier{
		&Piece{Color: "red",
			Point:  Point{7, 3},
			Status: true, Name: "RedSoldier"},
	}
	c.RedSoldier3 = &Soldier{
		&Piece{Color: "red",
			Point:  Point{7, 5},
			Status: true, Name: "RedSoldier"},
	}
	c.RedSoldier4 = &Soldier{
		&Piece{Color: "red",
			Point:  Point{7, 7},
			Status: true, Name: "RedSoldier"},
	}
	c.RedSoldier5 = &Soldier{
		&Piece{Color: "red",
			Point:  Point{7, 9},
			Status: true, Name: "RedSoldier"},
	}
}

// initMap 初始化m，顺便擦下屁股
func (c *Chessboard) initMap() {
	val := reflect.ValueOf(*c)
	for i := 0; i < val.NumField()-1; i++ {
		//p是Chessboard的字段
		p := val.Field(i).Interface()
		//APiece获取p的子字段*Piece

		piece := new(Piece)
		switch p.(type) {
		case *Chariot:
			APiece := reflect.ValueOf(*p.(*Chariot))
			piece = APiece.Field(0).Interface().(*Piece)
		case *Horse:
			APiece := reflect.ValueOf(*p.(*Horse))
			piece = APiece.Field(0).Interface().(*Piece)
		case *Cannon:
			APiece := reflect.ValueOf(*p.(*Cannon))
			piece = APiece.Field(0).Interface().(*Piece)
		case *Elephant:
			APiece := reflect.ValueOf(*p.(*Elephant))
			piece = APiece.Field(0).Interface().(*Piece)
		case *Soldier:
			APiece := reflect.ValueOf(*p.(*Soldier))
			piece = APiece.Field(0).Interface().(*Piece)
		case *King:
			APiece := reflect.ValueOf(*p.(*King))
			piece = APiece.Field(0).Interface().(*Piece)
		case *Guard:
			APiece := reflect.ValueOf(*p.(*Guard))
			piece = APiece.Field(0).Interface().(*Piece)
		}
		piece.chessboard = c
		piece.piece = p
		//fmt.Println("piece", piece)
		//fmt.Println(reflect.TypeOf(p))
		c.M[piece.Point] = piece
	}
}
